package com.example.demo.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;


@Aspect
@Component
public class LogInterceptor {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    //9、在controller之前的错误，及controller之后的错误，应该如何统一进行国际化输出？
    /**
     * 获取request实例
     * @return
     */
    @Pointcut("execution(!void com..*Controller.*(..))")
    public void serviceAspect(){

    }
    @Before("serviceAspect()")
    public void before(JoinPoint joinPoint){
        //日志记录
        long start = System.currentTimeMillis();
         logger.info("before===="+joinPoint);
    }

    @After("serviceAspect()")
    public void after(JoinPoint joinPoint) {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;
        HttpServletRequest request = sra.getRequest();
        String targetName=joinPoint.getTarget().getClass().getName();
        String methodName=joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        logger.info("after====="+targetName+""+methodName+arguments+joinPoint);

    }



}

